package com.io.io02;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Demo1 {
    public static void main(String[] args) throws Exception {
//        File file1 = new File("D:\\课程\\IO\\hello.txt");
//        System.out.println(file1.exists());
//        System.out.println(file1.isDirectory());
//        System.out.println(file1.isFile());

        File file1 = new File("D:\\课程\\\\IO\\");
//        String absolutePath = file1.getAbsolutePath();
//        String canonicalPath = file1.getCanonicalPath();
//        System.out.println(absolutePath);
//        System.out.println(canonicalPath);

//        System.out.println(file1.getName());
//        System.out.println(file1.getParent());
//        System.out.println(file1.getParentFile().getCanonicalPath());
//        long s = System.currentTimeMillis();    // 毫秒
//        Thread.sleep(1000);
//        long e = System.currentTimeMillis();
//        long 经过的毫秒 = e - s;
//        System.out.println(经过的毫秒);

//        File[] files = file1.listFiles();
//        System.out.println(Arrays.toString(files));

//        traversalBroadFirst(new File("D:\\课程\\IO"));

//        File[] files = File.listRoots();
//        System.out.println(Arrays.toString(files));

//        File file = new File("D:\\课程\\2022-06-29-2022火箭班-IO\\a\\b\\c\\d\\e\\f");
//        System.out.println(file.mkdirs());

        File file = new File("D:\\课程\\IO\\src\\nihao.txt");
//        File dest = new File("D:\\课程\\2022-06-29-2022火箭班-IO\\src\\bye.txt");
        File dest = new File("D:\\课程\\IO\\dest\\nihao.txt");

        file.renameTo(dest);
    }

    // 1. 深度优先的遍历
    private static void traversalDepthFirst(File dir) throws Exception {
        // 1. 找到这个目录下的所有孩子
        File[] files = dir.listFiles();
        if (files == null) {
            return;
        }

        // 2. 针对每个孩子，判断是目录还是文件
        for (File file : files) {
            if (file.isDirectory()) {
                // 如果确定是个目录，则继续递归去遍历处理
                System.out.println("[D] " + file.getCanonicalPath());
                traversalDepthFirst(file);
            } else {
                System.out.println("[F] " + file.getCanonicalPath());
            }
        }
    }

    // 2. 广度优先的遍历
    private static void traversalBroadFirst(File dir) throws Exception {
        // 使用队列进行层序优先的遍历
        Queue<File> queue = new LinkedList<>();
        queue.offer(dir);

        while (!queue.isEmpty()) {
            File poll = queue.poll();
            if (poll.isDirectory()) {
                System.out.println("[D] " + poll.getCanonicalPath());
            } else {
                System.out.println("[F] " + poll.getCanonicalPath());
            }

            if (poll.isDirectory()) {
                File[] files = poll.listFiles();
                if (files == null) {
                    continue;
                }

                for (File file : files) {
                    queue.offer(file);
                }
            }
        }
    }
}
